package com.epoint.mobileoa.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.epoint.mobileoa.bizlogic.Res;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.db.DBHelp;
import com.epoint.mobileoa.tools.PropertiesHelp;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;



public class PersonChooseAdapter extends BaseAdapter {
	
	class ViewHolder {
		public ImageView img;
		public TextView title;
		public TextView info;
		public CheckBox cbPerson;
	}

	private LayoutInflater mInflater;
	private List<Map<String, Object>> mData;
	private List<Map<String,String>> clist;
	private List<Map<String,String>> deptslist;
	private EditText et;
	private SuperView context;

	public PersonChooseAdapter(SuperView context,List<Map<String, Object>> mData,List<Map<String,String>> clist,List<Map<String,String>> deptslist,EditText et) {
		this.mInflater = LayoutInflater.from(context);
		this.context = context;
		this.mData = mData;
		this.clist = clist;
		this.deptslist = deptslist;
		this.et = et;
	}

	@Override
	public int getCount() {
		return mData.size();
	}

	@Override
	public Object getItem(int arg0) {
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	ViewHolder holder = null;
	
	@Override
	public View getView(final int position, View convertView,
			ViewGroup parent) {
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(Res.getLayoutInt("listviewwithbtnandimg"),
					null);
			holder.img = (ImageView) convertView.findViewById(Res.getIdInt("img"));
			holder.title = (TextView) convertView.findViewById(Res.getIdInt("title"));
			holder.info = (TextView) convertView.findViewById(Res.getIdInt("info"));
			holder.cbPerson = (CheckBox) convertView
					.findViewById(Res.getIdInt("cbPerson"));

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if(mData.get(position).get("info").equals("部门")){
			if(PropertiesHelp.getProperty(context, "ouchoose").equals("0")){
				holder.cbPerson.setVisibility(View.INVISIBLE);
			}
			
			if(((String) mData.get(position).get("title")).equals("新点软件")
					||((String) mData.get(position).get("title")).equals("客户人员")){
				holder.cbPerson.setVisibility(View.INVISIBLE);
			}
			
			if(context.getBackgroundStr().equals("background")){
				holder.title.setTextColor(context.getResources().getColor(com.epoint.mobileoa.R.color.white));
			}
			else{
				holder.title.setTextColor(context.getResources().getColor(com.epoint.mobileoa.R.color.black));
			}
			
			holder.img.setBackgroundResource(Res.getDrawableInt("contacts_ou"));
		}
		else{
			holder.cbPerson.setVisibility(View.VISIBLE);
			holder.img.setBackgroundResource(Res.getDrawableInt("contackt_user"));
			if(context.getBackgroundStr().equals("background")){
				holder.title.setTextColor(context.getResources().getColor(com.epoint.mobileoa.R.color.solid_yellow));
			}
			else{
				holder.title.setTextColor(context.getResources().getColor(com.epoint.mobileoa.R.color.blue));
			}
		}

		holder.title.setText((String) mData.get(position).get("title"));
		holder.info.setText((String) mData.get(position).get("dp"));

		final Map<String,String> map = new HashMap<String, String>();
		map.put("guid", mData.get(position).get("guid").toString());
		map.put("name", mData.get(position).get("title").toString());

		holder.cbPerson.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if(mData.get(position).get("info").equals("人员")){
					if(isChecked){
						if(!clist.contains(map)){
							clist.add(map);
						}
					}
					else{
						if(clist.contains(map)){
							clist.remove(map);
						}
					}
					String selectedName = "";
					String selectedGuid = "";
					int size = clist.size();
					if(size>0){
						for(int i=0;i<size;i++){
							if(i<size-1){
								selectedName += clist.get(i).get("name")+";";
								selectedGuid += clist.get(i).get("guid")+";";
							}
							else if(i==size-1){
								selectedName += clist.get(i).get("name");
								selectedGuid += clist.get(i).get("guid");
							}
						}
					}
					et.setText(selectedName);
				}
				else if(mData.get(position).get("info").equals("部门")){

					List<Object> objectlist = DBHelp.getAllUserUnderOu(context,map);
					ArrayList<Map<String,String>> list = (ArrayList<Map<String, String>>) objectlist.get(1);
					ArrayList<Map<String,String>> oulist = (ArrayList<Map<String,String>>) objectlist.get(0);

					if(isChecked){
						for(int i=0;i<oulist.size();i++){
							if(!deptslist.contains(oulist.get(i))){
								deptslist.add(oulist.get(i));
							}
						}
					}
					else{
						for(int i=0;i<oulist.size();i++){
							if(deptslist.contains(oulist.get(i))){
								deptslist.remove(oulist.get(i));
							}
						}
					}

					for(int i=0;i<list.size();i++){
						Map<String,String> map2 = new HashMap<String, String>();
						map2.put("guid", list.get(i).get("userguid"));
						map2.put("name", list.get(i).get("name"));

						if(isChecked){
							if(!clist.contains(map2)){
								clist.add(map2);
							}
						}
						else{
							if(clist.contains(map2)){
								clist.remove(map2);
							}
						}
						String selectedName = "";
						String selectedGuid = "";
						int size = clist.size();
						if(size>0){
							for(int j=0;j<size;j++){
								if(j<size-1){
									selectedName += clist.get(j).get("name")+";";
									selectedGuid += clist.get(j).get("guid")+";";
								}
								else if(j==size-1){
									selectedName += clist.get(j).get("name");
									selectedGuid += clist.get(j).get("guid");
								}
							}
						}
						et.setText(selectedName);
					}

				}
			}

		});

		if (clist.contains(map)||deptslist.contains(map)) {
			holder.cbPerson.setChecked(true);
		}
		else{
			holder.cbPerson.setChecked(false);
		}
		return convertView;
	}

}
